#ifndef WOLF_SDL_OK_TEST_OK
#define WOLF_SDL_OK_TEST_OK

#include <SDL.h>

#include "test.hpp"

namespace wolf
{
    /**
     * @brief Tests if sdl system is working fine
     *
     * It returns false to known errors, but it might not return for some others.
     *
     * The test is just to show an image on the screen and exit it when it is closed
     *  or the user press the ESCAPE key.
     **/
    class SdlOkTest: public Test
    {
    public:
        SdlOkTest(): Test( "SDL correct compile and link" ) {}
        ~SdlOkTest() {}
        bool run()
        {
            // create a new window
            SDL_Surface* screen = SDL_SetVideoMode( 640, 480, 16,
                                                    SDL_HWSURFACE | SDL_DOUBLEBUF );
            if ( !screen )
            {
                printf( "Unable to set 640x480 video: %s\n", SDL_GetError() );
                return false;
            }

            // load an image
            SDL_Surface* bmp = SDL_LoadBMP( "cb.bmp" );
            if ( !bmp )
            {
                printf( "Unable to load bitmap: %s\n", SDL_GetError() );
                return false;
            }

            // centre the bitmap on screen
            SDL_Rect dstrect;
            dstrect.x = ( screen->w - bmp->w ) / 2;
            dstrect.y = ( screen->h - bmp->h ) / 2;

            // program main loop
            bool done = false;
            while ( !done )
            {
                // message processing loop
                SDL_Event event;
                while ( SDL_PollEvent( &event ) )
                {
                    // check for messages
                    switch ( event.type )
                    {
                            // exit if the window is closed
                        case SDL_QUIT:
                            done = true;
                            break;

                            // check for keypresses
                        case SDL_KEYDOWN:
                        {
                            // exit if ESCAPE is pressed
                            if ( event.key.keysym.sym == SDLK_ESCAPE )
                                done = true;
                            break;
                        }
                    } // end switch
                } // end of message processing

                // DRAWING STARTS HERE

                // clear screen
                SDL_FillRect( screen, 0, SDL_MapRGB( screen->format, 0, 0, 0 ) );

                // draw bitmap
                SDL_BlitSurface( bmp, 0, screen, &dstrect );

                // DRAWING ENDS HERE

                // finally, update the screen :)
                SDL_Flip( screen );
            } // end main loop

            // free loaded bitmap
            SDL_FreeSurface( bmp );
            return true;
        }
    protected:

    private:

    };

}

#endif // WOLF_SDL_TEST_OK

